package cn.uce.srv.mq.rocket.listener;

import cn.uce.common.log.biz.IOperationLogBiz;
import cn.uce.common.log.entity.OperationLog;
import cn.uce.core.mq.api.IMqMessage;
import cn.uce.core.mq.api.MqConsumerContext;
import cn.uce.core.mq.api.exception.MqConsumeRollbackException;
import cn.uce.core.mq.api.listener.AbstractMqSingleMsgListener;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;

/**
 * 日志监听
 * @author Sunny
 * @Date 2020-07-06 10:35
 */
public class LoggerOutPutListener extends AbstractMqSingleMsgListener<String> {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private IOperationLogBiz iOperationLogBiz;

    @Override
    public void processMessage(IMqMessage<String> message, MqConsumerContext context) {
        try{
            if(logger.isWarnEnabled()){
                logger.warn("MQ接收到消息： topic:ccLogOutput,messageId:{},messageBody:{}",message.getMsgID(),message.getMessageBody());
            }
            OperationLog info = JSONObject.parseObject(message.getMessageBody(),OperationLog.class);
            iOperationLogBiz.addOperationLog(info);
        }catch (MqConsumeRollbackException e){
            logger.error("日志处理Rollback异常：",e);
            throw new MqConsumeRollbackException(e.getMessage(), e);
        }catch (Exception e){
            logger.error("日志处理异常：",e);
            throw new MqConsumeRollbackException(e.getMessage(),e);
        }
    }
}
